﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;

namespace Harmontronics.Service
{
    public class PlcSettingService : IPlcSettingService
    {
        private readonly IPlcSettingOperate _plcSettingOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public PlcSettingService(IUnitOfWork unitOfWork, IMapper mapper, IPlcSettingOperate plcSettingOperate)
        {
            _mapper = mapper;
            _plcSettingOperate = plcSettingOperate;
            _unitOfWork = unitOfWork;
        }
        public bool AddPlcSetting(PlcSettingModel plcSetting)
        {
            try
            {
                PlcSetting entity = _mapper.Map<PlcSetting>(plcSetting);
                _plcSettingOperate.AddPlcSetting(entity);
                _unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;

        }

        public bool DeletePlcSetting(string id)
        {
            try
            {
                PlcSetting entity = _plcSettingOperate.GetPlcSettingById(id);
                if (entity != null)
                {
                    _plcSettingOperate.DeletePlcSetting(entity);
                    _unitOfWork.Commit();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public List<PlcSettingModel> GetAllPlcSettings()
        {
            try
            {
                List<PlcSetting> entities = _plcSettingOperate.GetAllPlcSettingList();
                List<PlcSettingModel> modelList = new List<PlcSettingModel>();
                foreach (PlcSetting entity in entities)
                {
                    modelList.Add(_mapper.Map<PlcSettingModel>(entity));
                }
                return modelList;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<PlcSettingModel> GetPlcSettingByIp(string ip)
        {
            try
            {
                List<PlcSetting> entities = _plcSettingOperate.GetPlcSettingByIp(ip);
                List<PlcSettingModel> modelList = new List<PlcSettingModel>();
                foreach(PlcSetting entity in entities)
                {
                    modelList.Add(_mapper.Map<PlcSettingModel>(entity));
                }
                return modelList;
            }
            catch (Exception ex)
            {

            }
            return null;
        }



        public bool UpdatePlcSetting(PlcSettingModel plcsetting)
        {
            try
            {
                PlcSetting entity = _plcSettingOperate.GetPlcSettingById(plcsetting.PlcId);
                if (entity != null)
                {
                    _mapper.Map<PlcSettingModel, PlcSetting>(plcsetting, entity);
                    _plcSettingOperate.UpdatePlcSetting(entity);
                }
                _unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public PlcSettingModel GetPlcSettingById(string id)
        {
            try
            {
                PlcSetting entity = _plcSettingOperate.GetPlcSettingById(id);
                if (entity != null)
                {
                    return _mapper.Map<PlcSettingModel>(entity);
                }
            }
            catch (Exception ex)
            {

            }
            return null;
        }

        public bool BatchDelPlc(List<string> ids)
        {
            try
            {
                List<PlcSetting> entities = _plcSettingOperate.GetPlcSettingByIdList(ids);
                if (entities.Count > 0)
                {
                    _plcSettingOperate.BatchDelPlcSetting(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
